﻿using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace SP2010
{
    public class CustomSearchStringSerializer : SearchStringSerializer
    {
        public string Scope
        {
            get
            {
                return "Client";
            }
        }

        public IDictionary<string, string> YouSearchedFor;
        public IDictionary<string, string> YouSearchedForText;

        #region Custom Search Specific Values

        public string Property1 { get; set; }
        public string Property2 { get; set; }
     
        public bool? BooleanProperty1 { get; set; }
       
        public List<string> MultiValueProperty1 { get; set; }
      
        #endregion

        public CustomSearchStringSerializer() : base()
        {
            YouSearchedFor = new Dictionary<string, string>();
            YouSearchedForText = new Dictionary<string, string>();

            // Set a default search scope.
            BooleanProperty1 = null;

            MultiValueProperty1 = new List<string>();    
        }

        #region ISearchStringSerializer Members

        public override string GetSearchString()
        {
            StringBuilder queryBuilder = new StringBuilder();
            AddItemToQuery(ref queryBuilder, "Property1", this.Property1, false);
            AddItemToQuery(ref queryBuilder, "Property2", this.Property2);
            
            if (this.BooleanProperty1.HasValue && this.BooleanProperty1.Value)
            {
                AddItemToQuery(ref queryBuilder, "BooleanProperty1", this.BooleanProperty1);
            }
           
            AddItemToQuery(ref queryBuilder, "MultiProperty1", this.MultiValueProperty1);
          
            string retVal = string.Format("Scope:\"{1}\" {0}", queryBuilder.ToString(), this.Scope);
            return retVal;
        }

        /// <summary>
        /// This function can read the query string and hydrate the class properties
        /// </summary>
        /// <param name="queryString"></param>
        public override void LoadFromQueryString(NameValueCollection queryString)
        {
            //Property1
            List<string> checkInput = this.ParseQueryString(queryString, "Property1");
            if (checkInput.Count > 0)
            {
                Property1 = checkInput[0];
                YouSearchedFor.Add("Property1", Property1);
            }

            //Property2
            checkInput = this.ParseQueryString(queryString, "Property2");
            if (checkInput.Count > 0)
            {
                Property1 = checkInput[0];
                YouSearchedFor.Add("Property2", Property1);
            }

            //BooleanProperty1
            checkInput = this.ParseQueryString(queryString, "BooleanProperty1");
            if (checkInput.Count > 0)
            {
                if (checkInput[0].Equals("TRUE"))
                {
                    BooleanProperty1 = true;
                    YouSearchedFor.Add("BooleanProperty1", "True");
                }
                else
                {
                    BooleanProperty1 = false;
                    YouSearchedFor.Add("BooleanProperty1", "False");
                }
            }

            //MultiProperty1
            checkInput = this.ParseQueryString(queryString, "MultiProperty1");
            if (checkInput.Count > 0)
            {
                MultiValueProperty1 = checkInput;
                UpdateYouSearchedForString("MultiProperty1", MultiValueProperty1);
            }

            
        }

        public override void LoadFromQueryStringForText(NameValueCollection queryString)
        {   
          
        }

        public void UpdateYouSearchedForString(string property, List<string> PropertyValues)
        {
            string value = string.Empty;
            foreach (var item in PropertyValues)
            {
                value += item + " OR ";
            }

            // This will drop the last OR
            value = value.Substring(0, value.Length - 4);
            YouSearchedFor.Add(property, value);
        }

        public void UpdateYouSearchedForTextString(string property, List<string> PropertyValues)
        {
            string value = string.Empty;
            foreach (var item in PropertyValues)
            {
                value += item + " OR ";
            }

            // This will drop the last OR
            value = value.Substring(0, value.Length - 4);
            YouSearchedForText.Add(property, value);
        }
        #endregion

    }
}
